<?php

/**
 * CSwiftMailer class file.
 *
 * @author Alexandr Dorogikh (aka lexand)
 * @version 0.0
 * @copyright Copyright &copy; 2009 Alexandr Dorogikh
 *
 * Copyright (C) 2009 Alexandr Dorogikh.
 *
 * 	This program is free software: you can redistribute it and/or modify
 * 	it under the terms of the GNU Lesser General Public License as published by
 * 	the Free Software Foundation, either version 2.1 of the License, or
 * 	(at your option) any later version.
 *
 * 	This program is distributed in the hope that it will be useful,
 * 	but WITHOUT ANY WARRANTY; without even the implied warranty of
 * 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * 	GNU Lesser General Public License for more details.
 *
 * 	You should have received a copy of the GNU Lesser General Public License
 * 	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * For third party licenses and copyrights, please see Swift-4.0.4/LICENSE.LGPL
 *
 */

/**
 * Include the the Swift file.
 */
require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'Swift-4.0.4'.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'swift_required.php');

/**
 * CSwiftMailer is a simple wrapper for the Swift mailer library.
 * @see http://swiftmailer.org/
 *
 * @author Lexand
 * @package application.extensions.CSwiftMailer
 * @since 1.0
 */
class CSwiftMailer extends CComponent{
//***************************************************************************
// Configuration
//***************************************************************************

    /**
     * Default email address
     * @var string
     */
    public $fromEmail = 'dontreplay@domain.com';

    /**
     * Set default to Yii::app()->name
     * @var string
     */
    public $fromName = '';

    public $local = false;

    /**
     * Type of transprt.
     * Only 'sendmail' supported in current version
     * @var srting
     */
    public $transport = 'sendmail';

    /**
     * Default charset
     * @var string
     */
    public $charset = 'utf-8';

    /**
     * The path to the directory where the view for getView is stored. Must not
     * have ending dot.
     *
     * @var string
     */
    protected $pathViews = 'ext.swiftmailer.views';

    /**
     * The path to the directory where the layout for getView is stored. Must
     * not have ending dot.
     *
     * @var string
     */
    protected $pathLayouts = 'ext.swiftmailer.views.layouts';

    //***************************************************************************
    // Private properties
    //***************************************************************************

    /**
     * Swift mailer transport
     * @var Swift_SendmailTransport
     */
    private $_transport;

    /**
     * Swift mailer
     * @var Swift_Mailer
     */
    private $_mailer;

    /**
     * The internal Swift mailer message object.
     *
     * @var Swift_Message
     */
    private $_message;


    //***************************************************************************
    // Initialization
    //***************************************************************************

    /**
     * Init method for the application component mode.
     */
    public function init() {}

    /**
     * Constructor. Here the instance of Swift mailer is created.
     */
    public function __construct() {
        $this->fromName = Yii::app()->name;
        $this->initSwiftPreferences();
        if(!$this->local){
            $this->initTransport();
            $this->_mailer = Swift_Mailer::newInstance($this->_transport);
        }
    }

    //***************************************************************************
    // Setters and getters
    //***************************************************************************

    /**
     * Setter
     *
     * @param string $value pathLayouts
     */
    public function setPathLayouts($value) {
        if (!is_string($value) && !preg_match("/[a-z0-9\.]/i"))
            throw new CException(Yii::t('CSwiftMailer', 'pathLayouts must be a Yii alias path'));
        $this->pathLayouts = $value;
    }

    /**
     * Getter
     *
     * @return string pathLayouts
     */
    public function getPathLayouts() {
        return $this->pathLayouts;
    }

    /**
     * Setter
     *
     * @param string $value pathViews
     */
    public function setPathViews($value) {
        if (!is_string($value) && !preg_match("/[a-z0-9\.]/i"))
            throw new CException(Yii::t('CSwiftMailer', 'pathViews must be a Yii alias path'));
        $this->pathViews = $value;
    }

    /**
     * Getter
     *
     * @return string pathViews
     */
    public function getPathViews() {
        return $this->pathViews;
    }


    //***************************************************************************
    // Utilities
    //***************************************************************************

    /**
     * Generate an e-mail
     *
     * @param string $view the class
     * @param array $vars
     * @param string $layout
     * @param boolean $autoAssign
     */
    public function getView($view, $vars = array(), $layout = null, $autoAssign = true) {
        $body = Yii::app()->controller->renderPartial($this->pathViews.'.'.$view, array_merge($vars, array('content'=>$this)), true);
        if ($layout === null) {
            if($autoAssign) $this->_message->setBody($body);
        }
        else {
            $body = Yii::app()->controller->renderPartial($this->pathLayouts.'.'.$layout, array('content'=>$body), true);
            if($autoAssign) $this->_message->setBody($body);
        }
        return $body;
    }

    private function initTransport(){
        switch($this->transport){
            case 'sendmail':
                $this->_transport = Swift_SendmailTransport::newInstance(ini_get("sendmail_path"));
                break;
        }
    }

    private function initSwiftPreferences(){
        Swift_Preferences::getInstance()->setCharset('windows-1251');
    }

    /**
     * Send message
     * @param Swift Message $message
     */
    public function send($message=null){
        if($message === null) $message = $this->_message;
        if($this->local) {
            //!TODO: какойто код
            return true;
        }
        $this->_mailer->send($message);
    }

    /**
     * Create new Swift Message
     * @return Swift_Message
     */
    public function newMessage($fromEmail=null, $fromName=null){
        $this->_message = Swift_Message::newInstance();
        if($fromEmail===null) $fromEmail = $this->fromEmail;
        if($fromName===null) $fromName = $this->fromName;
        $this->_message->setFrom($fromEmail,$fromName);
        return $this->_message;
    }
}